app.controller('FileUploadCtrl', ['$scope', 'FileUploader', function($scope, FileUploader) {
	var fd = [];//new FormData();
	var uploader = $scope.uploader = new FileUploader({
        //url: 'scripts/controllers/file-upload.php'
		//method: 'POST',
		url: 'http://localhost:8080/upload',
		//headers: {'Content-Type': undefined},
		//headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
	    //transformRequest: 'transform',
        //data: []
        //headers: { 'Content-Type': 'multipart/form-data'},
        //headers: {'Content-Type': undefined}
		//headers: {'Content-Type': 'application/json; charset=UTF-8'},
        //withCredentials: false,
        //headers: {
        //  'Content-Type': undefined
        //},
        //transformRequest: angular.identity,
        responseType: "arraybuffer"
        //formData: fd
		//responseType: 'byteArray'
    });

    // FILTERS

    uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            return this.queue.length < 10;
        }
    });

    // CALLBACKS

    uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/, filter, options) {
        console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
        console.info('onAfterAddingFile', fileItem);
    };
    uploader.onAfterAddingAll = function(addedFileItems) {
        console.info('onAfterAddingAll', addedFileItems);
    };
    uploader.onBeforeUploadItem = function(item) {
        console.info('onBeforeUploadItem', item);
    };
    uploader.onProgressItem = function(fileItem, progress) {
        console.info('onProgressItem', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        console.info('onProgressAll', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        console.info('onSuccessItem', fileItem, response, status, headers);
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
        console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        console.info('onCompleteItem', fileItem, response, status, headers);
    };
    uploader.onCompleteAll = function() {
        console.info('onCompleteAll');
    };

    console.info('uploader', uploader);
}]);


//inject angular file upload directives and service.angular.module('myApp', ['angularFileUpload']);
app.controller('MyCtrl',[ '$scope', 'FileUploader', function($scope, FileUploader) {//$upload是依赖的upload模块的服务名称module.service("$upload",[]）;
$scope.onFileSelect = function($files) {    //$files: an array of files selected, each file has name, size, and type.
  for (var i = 0; i < $files.length; i++) {      
	  var file = $files[i];
	  alert(file);
	  $scope.upload = FileUploader.upload({
		  url: 'server/upload/url', //upload.php script, node.js route, or servlet url
		  //method: 'POST' or 'PUT',
		  //headers: {'header-key': 'header-value'},
		  //withCredentials: true,
		  data: {myObj: $scope.myModelObj},
		  file: file, // or list of files ($files) for html5 only
		  //fileName: 'doc.jpg' or ['1.jpg', '2.jpg', ...] // to modify the name of the file(s)
		  // customize file formData name ('Content-Disposition'), server side file variable name. 
		  //fileFormDataName: myFile, //or a list of names for multiple files (html5). Default is 'file' 
		  // customize how data is added to formData. See #40#issuecomment-28612000 for sample code
		  //formDataAppender: function(formData, key, val){}
	  }).progress(function(evt) {       
		  console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
	  }).success(function(data, status, headers, config) {        // file is uploaded successfully
		  console.log(data);
	  }).error(function(data, status, headers, config) {
		  //失败处理函数
		  console.log('上传失败');
	  });      //.error(...)
	  //.then(success, error, progress); 
	  // access or attach event listeners to the underlying XMLHttpRequest.
	  //.xhr(function(xhr){xhr.upload.addEventListener(...)})
  	}    /* alternative way of uploading, send the file binary with the file's content-type.       Could be used to upload files to CouchDB, imgur, etc... html5 FileReader is needed.        It could also be used to monitor the progress of a normal http post/put request with large data*/
  	// $scope.upload = $upload.http({...})  see 88#issuecomment-31366487 for sample code.
};
}]);